from util import *
from message import *
import sys_server

class PgObject(object):
    base_class = None
    base_msgmap = None
    msg_map = {}
	
    def __init__(self, id = -1, background = Color.BLUE,
                 foreground = Color.RED, rect = (0,0,0,0)):
        self.background = background
        self.foreground = foreground
        self.rect = rect
        self.parent = None
        self.id = id
        self.is_focused = False
	base_class =  self.__class__.__bases__[0]
        if hasattr(self.base_class, 'msg_map'):
            base_msgmap =  self.base_class.msg_map
     
    def message(self, msg):
        msgmap = self.get_msgmap(self)
        base = self
        while msgmap:
            if msgmap.has_key(msg.type):
                if msg.type == MSG_NOTIFY and msgmap[msg.type].has_key(msg.id):
                    msgmap[msg.type][msg.id](self, msg)
                elif msg.type == MSG_DRAW:
                    msgmap[msg.type](self, sys_server)
                elif msg.type == MSG_TIMER or msg.type == MSG_LOSE_FOCUS or msg.type == MSG_GET_FOCUS:
                    msgmap[msg.type](self)
                else:
                    msgmap[msg.type](self, msg)
                return True
            base = base.base_class
            msgmap = self.get_msgmap(base)
        return False
    
    def draw_client(self, canvas):
        x,y,w,h = self.rect
        canvas.fill_rect(self.rect,self.background)
        canvas.draw_line((x,y),(x,y+h))
        canvas.draw_line((x,y),(x+w,y))
        canvas.draw_line((x+w,y),(x+w,y+h))
        canvas.draw_line((x,y+h),(x+w,y+h))

    def is_contain(self, point):
        x,y,w,h = self.rect
        px,py = point
        if px >= x and py >= y and px <= x+w and py <= y+h:
            return True
        else:
            return False
        
    def notify_parent(self):
        self.parent.message(Msg(MSG_NOTIFY, None, None, self.id))
        
    @staticmethod
    def get_msgmap(cls):
        if hasattr(cls, 'msg_map'):
            return cls.msg_map
        else:
            return False
    
    def get_client_rect(self):
        return rect

print PgObject.base_class
